package com.hunan.yllxy.util;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hunan.yllxy.vo.PageQuery;
import com.hunan.yllxy.vo.PageResult;

/**
 * @Author: 刘华
 * @Date: 2021/5/31 11:47
 * @Version 1.0
 */
public class PageResultUtils {
    public static PageResult generatePageResult(Page page, PageQuery pageQuery) {
        PageResult pageResult = new PageResult();
        pageResult.setData(page.getRecords());
        pageResult.setTotal(Long.valueOf(page.getTotal()).intValue());
        pageResult.setPageNum(pageQuery.getPageNum());

        pageResult.setPageSize(pageQuery.getPageSize());
        int total = Long.valueOf(page.getTotal()).intValue();
        int totalPage = (Long.valueOf(page.getTotal()).intValue() == 0 ? 0 : ((total + pageQuery.getPageSize() - 1) / pageQuery.getPageSize()));
        pageResult.setTotalPage(totalPage);
        return pageResult;
    }

    public static PageResult generatePageResult(Page page) {
        PageResult pageResult = new PageResult();
        pageResult.setData(page.getRecords());
        pageResult.setTotal(Long.valueOf(page.getTotal()).intValue());
        int pageNum = Long.valueOf(page.getCurrent()).intValue();
        int size = Long.valueOf(page.getSize()).intValue();
        pageResult.setPageNum(pageNum);
        pageResult.setPageSize(size);
        int total = Long.valueOf(page.getTotal()).intValue();
        int totalPage = (Long.valueOf(page.getTotal()).intValue() == 0 ? 0 : ((total + size - 1) / size));
        pageResult.setTotalPage(totalPage);
        return pageResult;
    }

    public static PageResult generatePageResult(Object o, Integer total, PageQuery pageQuery) {
        PageResult pageResult = new PageResult();
        pageResult.setData(o);
        pageResult.setTotal(total);
        pageResult.setPageNum(pageQuery.getPageNum());
        pageResult.setPageSize(pageQuery.getPageSize());
        int totalPage = ((total == null || total == 0) ? 0 : total) == 0 ? 0 : ((total + pageQuery.getPageSize() - 1) / pageQuery.getPageSize());
        pageResult.setTotalPage(totalPage);
        return pageResult;
    }

    public static <T> Page getPage(PageQuery pageQuery, T t) {
        Page<T> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        page.setSearchCount(true);
        return page;
    }
}
